from django.shortcuts import render
from django.views.generic.base import View
from jmbase.views import JMJsonBaseView

# Create your views here.

from others.models import Banner, OnlineBook
from product.models import Product, ProductInstance

class BannerView(JMJsonBaseView):
    def get(self, request, data):

        bls = Banner.objects.all()

        result = {
            "ban_list": [
                {
                    "pic": b.img.url
                } for b in bls
            ]
        }

        return result

class ProductCategoryListView(JMJsonBaseView):

    def get(self, request, data):
        ps = Product.objects.all()

        result = {
            "prod_cate_list": [
                {
                    "icon": p.icon.url,
                    "title": p.name,
                    "desc": [line for line in p.desc.split('=', maxsplit=1)]
                } for p in ps
            ]
        }

        return result

class ProductShow(JMJsonBaseView):
    def post(self, request, data:dict):

        pro_type = data.get('type')
        if not pro_type:
            return {}

        if pro_type == 'all':
            ps = ProductInstance.objects.all()
        else:
            ps = ProductInstance.objects.filter(type=pro_type)

        result = {
            "prod_list": [
                {
                    "pic": p.main_img.url
                } for p in ps
            ]
        }

        return result

    def get(self, request, data):
        # for test
        result = {
            "prod_list": [
                {
                    "pic": "/images/pro1"
                },
                {
                    "pic": "/images/pro1"
                },
                {
                    "pic": "/images/pro1"
                },
                {
                    "...": "..."
                }
            ]
        }

        return result

from others.models import Declaration
class AboutUs(JMJsonBaseView):
    def get(self, request, data):

        content = Declaration.objects.all()[0]

        result = {
            'content': content.about_us
        }

        return result

class JMNewsView(JMJsonBaseView):
    def get(self, request, data):
        result = {
            "hot_list": [
                {
                    "pic": "/images/hot1.png"
                },

            ],
            "news_list": [
                {
                    "title": "这个微信活动是如何6天增6",
                    "desc": "一场名为“班长到我出门(｡･∀･)ﾉﾞ嗨”的活动，中秋节前席卷北京"
                },
                {
                    "title": "这个微信活动是如何6天增6",
                    "desc": "一场名为“班长到我出门(｡･∀･)ﾉﾞ嗨”的活动，中秋节前席卷北京"
                },

            ]*3
        }

        return result


class OnlineBookView(JMJsonBaseView):
    def get(self, request, data):

        return {}

    def post(self, request, data:dict):

        if data:
            name = data.get('name')
            phone = data.get('phone')
            content = data.get('content')

            if name and phone and content:
                ob = OnlineBook(name=name, phone=phone, content=content)
                ob.save()

        return {}